package org.zoomdev.zoom.dao.structs.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zoomdev.zoom.common.exceptions.ZoomException;
import org.zoomdev.zoom.dao.Dao;
import org.zoomdev.zoom.dao.impl.SimpleSelect;
import org.zoomdev.zoom.dao.structs.ColumnMeta;
import org.zoomdev.zoom.dao.structs.TableMeta;
import org.zoomdev.zoom.dao.utils.DaoUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MysqlDatabaseStruct extends BaseDatabaseStruct {

    private static final Logger log = LoggerFactory.getLogger(Dao.class);

    public MysqlDatabaseStruct(Dao dao, String databaseName) {
        super(dao, databaseName);
    }

    @Override
    protected TableMeta fill(TableMeta tableMeta, Connection connection, SimpleSelect select) throws SQLException {
        String sql = "SELECT TABLE_COMMENT AS COMMENT,TABLE_NAME as NAME from information_schema.tables where table_schema=? AND TABLE_NAME=?";
        DaoUtils.visit(connection, data -> {
            try {
                tableMeta.setName(data.getString("NAME"));
                tableMeta.setComment(data.getString("COMMENT"));
            } catch (Exception t) {
                throw new ZoomException(t);
            }
        }, sql, name, tableMeta.getName());


        sql = "SELECT TABLE_NAME,COLUMN_NAME,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,COLUMN_KEY,EXTRA,COLUMN_COMMENT,COLUMN_DEFAULT " +
                "FROM information_schema.columns WHERE table_schema=? and TABLE_NAME=?";
        DaoUtils.visit(connection, data -> {
            String column = data.getString("COLUMN_NAME");
            ColumnMeta columnMeta = tableMeta.getColumn(column);
            if (columnMeta == null) {
                log.warn("找不到对应的字段:" + column);
                return;
            }
            fill(tableMeta.getName(), columnMeta, data);
        }, sql, name, tableMeta.getName());

        return tableMeta;
    }


    /**
     * @param tableName
     * @param columnMeta
     * @param data
     */
    private void fill(String tableName, ColumnMeta columnMeta, ResultSet data) throws SQLException {
        //    columnMeta.setTable(tableName);
        //    columnMeta.setComment(data.getString("COLUMN_COMMENT"));
        // 常用的
        columnMeta.setAutoIncrement(data.getString("EXTRA").equals("auto_increment"));
        String key = data.getString("COLUMN_KEY");
        if (key.equals("PRI")) {
            columnMeta.setKeyType(ColumnMeta.KeyType.PRIMARY);
        } else if (key.equals("UNI")) {
            columnMeta.setKeyType(ColumnMeta.KeyType.UNIQUE);
        } else if (key.equals("MUL")) {
            columnMeta.setKeyType(ColumnMeta.KeyType.INDEX);
        }
//        columnMeta.setDefaultValue(data.getString("COLUMN_DEFAULT"));
//        columnMeta.setNullable(data.getString("IS_NULLABLE").equals("YES"));
//        columnMeta.setMaxLength(data.getInt("CHARACTER_MAXIMUM_LENGTH"));
//        columnMeta.setRawType(data.getString("DATA_TYPE"));
    }
}
